package cn.iocoder.yudao.module.system.util;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class IdsUtils {

    //获取heard中的参数
    protected Map<String, String> getRequestHeaderMap(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        Map<String, String> headerMap = new HashMap<>(8);
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            headerMap.put(name.toLowerCase(), request.getHeader(name));
        }
        return headerMap;
    }

    public JSONObject findUserBySSOID(String sessionid) {
        //public JSONObject findUserBySSOID(HttpServletRequest req, LoginBody loginBody) {
        IdsConfig idsConfig = new IdsConfig();
        String idsloginurl = idsConfig.getIdsServiceUrl() + "/service?idsServiceType=httpssoservice&serviceName=findUserBySSOID";
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        //String coSessionId = req.getSession().getId();
        map.add("coSessionId", sessionid);
        //map.add("ssoSessionId", loginBody.getIdsToken());
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        map.add("coAppName", idsConfig.getAppName());
        map.add("type", "json");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(idsloginurl, request, String.class);
        return JSONObject.parseObject(response.getBody());
    }


    public static String getUserNameFromIds(JSONObject result) {
        IdsConfig idsConfig = new IdsConfig();
        String mobile = result.getJSONObject("data").getJSONObject("user").getString("mobile");
        String userName = result.getJSONObject("data").getJSONObject("user").getString("userName");
        //手机号为空就判断用户名
        if (StrUtil.isEmpty(mobile)) {
            if (userName.contains(idsConfig.getSeparator() + idsConfig.getAppName())) {
                userName = userName.replace(idsConfig.getSeparator() + idsConfig.getAppName(), "");
            } else {
                System.out.println("用户名不合规！！！");
            }
        } else {
            if (StrUtil.isNotEmpty(result.getJSONObject("data").getJSONObject("user").getString("IDSEXT_appusername"))) {
                JSONObject userNames = result.getJSONObject("data").getJSONObject("user").getJSONObject("IDSEXT_appusername");
                userName = userNames.getString(idsConfig.getAppName());
            }
        }
        System.out.println("=====================userName=======================");
        System.out.println(userName);
        return userName;
    }


}
